package com.zero.manage.web.components.query;

import cn.hutool.core.util.StrUtil;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.Predicate;
import com.zero.manage.web.components.entity.rbac.QSysUserEntity;
import com.zero.manage.web.utils.CommonConst;
import com.zero.manage.web.components.base.BaseQuery;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.LinkedHashMap;

@Getter
@Setter
@ApiModel("系统用户-查询条件")
public class SysUserQuery extends BaseQuery {
    @ApiModelProperty("用户名")
    private String name;

    @ApiModelProperty("角色名称")
    private String roleName;

    @Override
    public Predicate predicate() {
        QSysUserEntity qSysUserEntity = QSysUserEntity.sysUserEntity;
        //处理超级用户
        Predicate predicate = qSysUserEntity.name.ne(CommonConst.SUPER_USER);
        //执行动态条件拼装
        predicate = StrUtil.isBlank(name) ? predicate : ExpressionUtils.and(predicate, qSysUserEntity.name.contains(name));
        predicate = StrUtil.isBlank(roleName) ? predicate : ExpressionUtils.and(predicate, qSysUserEntity.roles.any().name.contains(roleName));
        return predicate;
    }

    /**
     * 后端固定排序
     *
     * @return
     */
    @Override
    protected LinkedHashMap<String, String> initSort() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("createTime", Order.DESC.name());
        return map;
    }


}

